﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Web.Mvc;
using System.Data.Entity;
using System.Linq;
using System.Data;
using System.Web.Security;
using CTMOES;
using CTMOES.Models;
using CTMOES.BLL;
using System;

namespace CTMOES.Controllers
{
    public class ExamController : Controller
    {
        private readonly CTMDBEntities _db = new CTMDBEntities();
        //
        // GET: /Exam/

        public ActionResult Index()
        {
            if (Membership.GetUser() != null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }

        public ActionResult Exam(string paperID)
        {
            if (Membership.GetUser() != null)
            {

                return View(ExamBusiness.GetPaper(Guid.Parse(paperID)));
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

        }

        [HttpPost]
        public ActionResult Exam(FormCollection collection)
        {
            // 单选题获取答案            
            Guid pID = Guid.Parse(Request["paperID"].ToString());
            var tmp = _db.Paper.Where(p => p.ID ==pID).FirstOrDefault();            
            Dictionary<Guid, Guid> choiceAns = new Dictionary<Guid, Guid>();
            foreach (var item in Request.Form)
            {
                try
                {
                    Guid temp = Guid.Parse(Request.Form[item as string] as string);
                    choiceAns.Add(Guid.Parse(item as string), temp);
                }
                catch (System.Exception e)
                {
                }
            }

            // 多选题和判断题获取答案
            var select = from x in Request.Form.AllKeys where Request.Form[x].Contains("false") select x;
            Dictionary<Guid, bool> multiAns = new Dictionary<Guid, bool>();
            foreach (var item in select)
            {
                if (Request.Form[item].Contains("true"))
                    multiAns.Add(Guid.Parse(item as string), true);
                else
                    multiAns.Add(Guid.Parse(item as string), false);
            }
            int score = 0;
            CTMOES.Paper paper = _db.Paper.Where(p => p.ID == pID).FirstOrDefault();

            score += ExamBusiness.CalculateChoice(choiceAns,Convert.ToInt32(paper.ChoiceValue));
            score += ExamBusiness.CalculateMultiChoice(multiAns, Convert.ToInt32(paper.MultiChoiceValue)); 
            CTMOES.Score newScore=new Score();
            newScore.ID = Guid.NewGuid();
            newScore.PaperId = paper.ID;
            newScore.StudentId =Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());
            newScore.StudentScore = score;
            newScore.Date = DateTime.Now;
            _db.Score.AddObject(newScore);
            _db.SaveChanges();
            return RedirectToAction("Result",new {Score=score});
            MembershipUser user = Membership.GetUser();
            ;
        }
        public ActionResult Result(float Score)
        {
            ViewData["Score"] = Score;     
            return View();
        }        
    }
}